"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库操作系统
Case Name   : global目录下执行备机重建
Create At   : 2020/10/14
@zou_jialiang0505328126
Description :
    1.cd 到global路径下
    2.备机在当前路径下全量重建
    3.查询数据库状态（若在global路径下查询，需要刷新该目录，因为在build成功后该目录删除后重建了）
    4.备机在当前路径下增量重建
    5.查询数据库状态
Expect      :
    1.cd 到global路径下成功
    2.备机在当前路径下全量重建成功
    3.查询数据库状态（若在global路径下查询，需要刷新该目录，因为在build成功后该目录删除后重建了）
    4.备机在当前路径下增量重建
    5.查询数据库状态
History     :
    modified ：2021-1-26 by @peilinqian ；修改build调用方式，修改整个脚本；
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DbsysCase085(unittest.TestCase):
    pri_sh = CommonSH('PrimaryDbUser')
    sta_1_sh = CommonSH('Standby1DbUser')

    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('----this is setup----')
        self.global_path = os.path.join(macro.DB_INSTANCE_PATH, 'global')
        self.standby1_dbuser = Node(node='Standby1DbUser')
        self.constant = Constant()
        self.log.info('----Opengauss_Reliability_Dbsys_Case085:初始化----')

    def test_main(self):
        self.log.info('----cd .../global; gs_ctl build full ----')
        build_cmd = f'''cd {self.global_path};
            pwd;
            source {macro.DB_ENV_PATH}; 
            gs_ctl build -D {macro.DB_INSTANCE_PATH} -b full -t 600;
            '''
        self.log.info(build_cmd)
        build_result = self.standby1_dbuser.sh(build_cmd).result()
        self.log.info(build_result)
        self.assertIn(self.constant.BUILD_SUCCESS_MSG, build_result)

        self.log.info('----check opengauss status----')
        self.log.info('----查看主机query，同步是否正常----')
        self.pri_sh.check_location_consistency('primary', 3)

        result = self.sta_1_sh.getDbClusterStatus('status')
        self.assertTrue(result)

        self.log.info('----cd .../global; gs_ctl build incremental----')
        build_cmd = f'''cd {self.global_path};
            pwd;
            source {macro.DB_ENV_PATH}; 
            gs_ctl build -D {macro.DB_INSTANCE_PATH} -b incremental -t 600;
            '''
        self.log.info(build_cmd)
        build_result = self.standby1_dbuser.sh(build_cmd).result()
        self.log.info(build_result)
        self.assertIn(self.constant.BUILD_SUCCESS_MSG, build_result)

        self.log.info('----check opengauss status----')
        self.log.info('----查看主机query，同步是否正常----')
        self.pri_sh.check_location_consistency('primary', 3)

        status_result = self.sta_1_sh.getDbClusterStatus('status')
        self.assertTrue(status_result)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        self.log.info('----Opengauss_Reliability_Dbsys_Case085：执行结束----')
